<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/19 22:26
 */

namespace module\chat\controller\admin;

use app\library\Controller;
use module\chat\enum\RoleEnum;
use module\chat\front\form\AssistantForm;
use mof\annotation\AdminPerm;
use mof\annotation\Inject;
use mof\annotation\InjectLogic;
use mof\ApiResponse;
use mof\Logic;
use think\response\Json;

#[AdminPerm(
    title: 'AI助手管理', url: 'chat/assistant', actions: '*',
    sort: 3, icon: 'Discount', group: 'main'
)]
class Assistant extends Controller
{
    #[Inject]
    protected AssistantForm $form;

    #[InjectLogic(\module\chat\model\Assistant::class)]
    protected Logic $logic;

    public function index(): Json
    {
        return ApiResponse::success(
            $this->logic->paginate(
                $this->request->searcher()->with(['category'])
            )
        );
    }

    public function read($id): Json
    {
        return ApiResponse::success($this->logic->read($id));
    }

    public function create(): Json
    {
        return ApiResponse::success($this->form->build());
    }

    public function save(): Json
    {
        return ApiResponse::success($this->logic->save(
            $this->form->withFixed(['type' => RoleEnum::ASSISTANT->value])->get()
        ));
    }

    public function edit($id): Json
    {
        return ApiResponse::success($this->form->build($this->logic->read($id)));
    }

    public function update($id): Json
    {
        $model = $this->logic->update(
            $id, $this->form->withScene('edit')->withFixed(['id' => $id])->get()
        );
        return ApiResponse::success($model);
    }

    public function delete($id): Json
    {
        $this->logic->delete($id);
        return ApiResponse::success();
    }
}